package qs.openxt.libs.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import qs.openxt.libs.R;
import qs.openxt.libs.util.StringUtil;

/**
 * Created by 颖杰_2 on 2016-07-15.
 */
public class UIAlertView extends Dialog {
    public UIAlertView(Context context) {
        super(context);
    }

    public UIAlertView(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context; // 上下文对象
        private String title; // 对话框标题
        private String message; // 对话框内容
        private String confirm_btnText; // 按钮名称“确定”
        private String cancel_btnText; // 按钮名称“取消”
        private String neutral_btnText; // 按钮名称“隐藏”
        private View contentView; // 对话框中间加载的其他布局界面
        /* 按钮监听事件 */
        private OnClickListener confirm_btnClickListener;
        private OnClickListener cancel_btnClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /* 设置对话框信息 */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置对话框界面
         *
         * @param v View
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param confirm_btnText
         * @return
         */
        public Builder setPositiveButton(int confirm_btnText,
                                         OnClickListener listener) {
            this.confirm_btnText = (String) context.getText(confirm_btnText);
            this.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * Set the positive button and it's listener
         *
         * @param confirm_btnText
         * @return
         */
        public Builder setPositiveButton(String confirm_btnText,
                                         OnClickListener listener) {
            this.confirm_btnText = confirm_btnText;
            this.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @return
         */
        public Builder setNegativeButton(int cancel_btnText,
                                         OnClickListener listener) {
            this.cancel_btnText = (String) context.getText(cancel_btnText);
            this.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * Set the negative button and it's listener
         *
         * @return
         */
        public Builder setNegativeButton(String cancel_btnText,
                                         OnClickListener listener) {
            this.cancel_btnText = cancel_btnText;
            this.cancel_btnClickListener = listener;
            return this;
        }

        TextView titleT;
        LinearLayout message_d;
        TextView messageT;
        Button cancelBtn;
        Button confirmBtn;
        View second_line;

        public UIAlertView create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            // instantiate the dialog with the custom Theme
            final UIAlertView dialog = new UIAlertView(context, R.style.uialertview_style);
            View layout = inflater.inflate(R.layout.uialertview, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            titleT = (TextView)layout.findViewById(R.id.title);
            message_d = (LinearLayout) layout.findViewById(R.id.message_d);
            messageT = (TextView)layout.findViewById(R.id.message);
            cancelBtn = (Button)layout.findViewById(R.id.cancel_btn);
            confirmBtn = (Button)layout.findViewById(R.id.confirm_btn);
            second_line = layout.findViewById(R.id.second_line);

            // set the dialog title
            if(title != null)
                titleT.setText(title);
            else
                titleT.setVisibility(View.GONE);
            //titleT.getPaint().setFakeBoldText(true);

            if(!StringUtil.isNull(confirm_btnText)){
                confirmBtn.setText(confirm_btnText);
                confirmBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(confirm_btnClickListener != null){
                            confirm_btnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }else {
                            dialog.dismiss();
                        }
                    }
                });
            }else{
                confirmBtn.setVisibility(View.GONE);
            }

            if(!StringUtil.isNull(cancel_btnText)){
                cancelBtn.setText(cancel_btnText);
                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(cancel_btnClickListener != null){
                            cancel_btnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }else {
                            dialog.dismiss();
                        }
                    }
                });
            }else{
                cancelBtn.setVisibility(View.GONE);
            }
            if(StringUtil.isNull(cancel_btnText) && !StringUtil.isNull(confirm_btnText)){
                confirmBtn.setBackgroundResource(R.drawable.single_btn_select);
                second_line.setVisibility(View.GONE);
            }
            if(StringUtil.isNull(confirm_btnText) && !StringUtil.isNull(cancel_btnText)){
                cancelBtn.setBackgroundResource(R.drawable.single_btn_select);
                second_line.setVisibility(View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                LinearLayout child = (LinearLayout) layout.findViewById(R.id.message_d);
                child.removeAllViews();
                child.addView(contentView, new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);

            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);

            return dialog;
        }

    }
}
